{Procedures in this unit provide bounded edit capability- checking user input}
{for legality (is it a valid number?) and range before accepting the value}
{SEC     January 2007}

unit Editor;

  {$R+}     {Range checking turned on}

interface

uses    SysUtils;

{BoundStr2Int()   Bounded string-to-integer conversion}
{This procedure converts a text string StringIn and converts it to an integer}
{If all is well, the integer is placed in IntegerOut and the function returns 0}
{If the string is not recognized as a number, it returns a 1 }
{ and does not change the value of IntegerOut}
{If the string recognizes a number, }
{   but the value is outside the range MinInt-Maxint}
{   then the function returns a 2 and does not change the value of IntegerOut}

Function BoundStr2Int(TextIn : string; Var IntegerOut : Integer;
         MinInt, MaxInt : integer) : integer;

{BoundStr2Float()   Bounded string-to-float conversion}
{This procedure converts a text string Text and converts it to a real number}
{If all is well, the number is placed in RealOut and the function returns 0}
{If the string is not recognized as a number, it returns a 1 }
{ and does not change the value of RealOut}
{If the string recognizes a number, }
{   but the value is outside the range MinBound-MaxBound}
{   then the function returns a 2 and does not change the value of RealOut}

Function BoundStr2Float(TextIn : string; Var RealOut : double;
         MinBound, MaxBound : double) : integer;

{WriteIntEdit uses BoundStr2Int to convert a string to integer form, }
{then returns a string corresponding to that integer}

Function WriteIntEdit(InText  : String;
         Var InNum : integer;
         Low, High: integer): string;

{WriteRealEdit uses BoundStr2Float to convert a string to real number form, }
{then returns a string corresponding to that number}

Function WriteRealEdit(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;

{WriteRealEditFix uses BoundStr2Float to convert a string to real number form, }
{then returns a string corresponding to that number in fixed-decimal format}

Function WriteRealEditFix(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;

Function WriteRealEditExp(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;


{****************************************************************************}
{****************************************************************************}

implementation


{BoundStr2Int()   Bounded string-to-integer conversion}
{This procedure converts a text string StringIn and converts it to an integer}
{If all is well, the integer is placed in IntegerOut and the function returns 0}
{If the string is not recognized as a number, it returns a 1 }
{ and does not change the value of IntegerOut}
{If the string recognizes a number, }
{   but the value is outside the range MinInt-Maxint}
{   then the function returns a 2 and does not change the value of IntegerOut}

Function BoundStr2Int(TextIn : string; Var IntegerOut : Integer;
         MinInt, MaxInt : integer) : integer;

var  Temporary : integer;       { holds the converted numeric value from Val}
     code : integer;            { holds the error code from Val }
  begin
    {First, call the Val procedure for conversion}
    Val(TextIn, Temporary, code);

     If (code <> 0) then begin             {check for readable string}
       Result := 1;
       Exit;
     end;

    {Then check for 'out-of-bounds' violation}
    If (Temporary < MinInt) or (Temporary > MaxInt) then
        BoundStr2Int := 2
       { if no violation, return the new integer}
       else begin
        BoundStr2Int := 0;
        IntegerOut := Temporary;
        end;
  end;                          {End of BoundStr2Int}

{BoundStr2Float()   Bounded string-to-float conversion}
{This procedure converts a text string Text and converts it to a real number}
{If all is well, the number is placed in RealOut and the function returns 0}
{If the string is not recognized as a number, it returns a 1 }
{ and does not change the value of RealOut}
{If the string recognizes a number, }
{   but the value is outside the range MinBound-MaxBound}
{   then the function returns a 2 and does not change the value of RealOut}

Function BoundStr2Float(TextIn : string; Var RealOut : double;
         MinBound, MaxBound : double) : integer;

var  Temporary : double;       { holds the converted numeric value from Val}
     code : integer;            { holds the error code from Val }
  begin
    {First, call the Val procedure for conversion}
    Val(TextIn, Temporary, code);

    If (code <> 0) then begin             {check for readable string}
       Result := 1;
       Exit;
     end;

    {Then check for 'out-of-bounds' violation}
    If (Temporary < MinBound) or (Temporary > MaxBound) then
        Result := 2

       { if no violation, return the new integer}
       else begin
        Result := 0;
        RealOut := Temporary;
        end;
  end;                          {End of BoundStr2Float}

{WriteIntEdit uses BoundStr2Int to convert a string to integer form, }
{then returns a string corresponding to that integer}

Function WriteIntEdit(InText  : String;
         Var InNum : integer;
         Low, High: integer): string;

Var   code : integer;
begin
  code := BoundStr2Int(InText, InNum, Low, High);
  Result := IntToStr(InNum);
end;

{WriteRealEdit uses BoundStr2Float to convert a string to real number form, }
{then returns a string corresponding to that number in 'best' format}

Function WriteRealEdit(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;
Var   code : integer;
begin
  code := BoundStr2Float(InputText, RealOut, MinB, MaxB);
  Result := FloatToStr(RealOut);
 end;

{WriteRealEditFix uses BoundStr2Float to convert a string to real number form, }
{then returns a string corresponding to that number in exponential format}

Function WriteRealEditFix(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;

Var   code : integer;
begin
  code := BoundStr2Float(InputText, RealOut, MinB, MaxB);
  Result := FloatToStrF(RealOut, ffFixed, 8, 3);
 end;

Function WriteRealEditExp(InputText : String; Var RealOut : double;
                       MinB, MaxB : double) : string;

Var   code : integer;
begin
  code := BoundStr2Float(InputText, RealOut, MinB, MaxB);
  Result := FloatToStrF(RealOut, ffExponent, 4, 3);
 end;




end.
